package com.lx.txx.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lx.bean.Result;
import com.lx.txx.dao.TxxLeaseMapper;
import com.lx.txx.entity.TxxLease;
import com.lx.txx.entity.TxxLeaseEx;
import com.lx.txx.entity.TxxUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 天下行租车-用车表-10 前端控制器
 * </p>
 *
 * @author lx
 * @since 2024-12-30
 */
@RestController
@RequestMapping("/txxLease")
public class TxxLeaseController {

    @Autowired
    TxxLeaseMapper txxLeaseMapper;

    @Autowired
    JdbcTemplate jdbcTemplate;


    @PostMapping("leaseCar")
    public Result leaseCar(@RequestBody TxxLease txxLease, @SessionAttribute TxxUser loginedUser){
        int uid = loginedUser.getId();
        txxLease.setUid(uid);
        txxLease.setOrderTime(LocalDateTime.now());
        txxLease.setState("待提车");
        txxLeaseMapper.insert(txxLease);
        return new Result(1, "租赁成功", null);
    }

    @RequestMapping("getLeasedays")
    public Result getLeasedays(int cid){
        String sql = "select days from txx_lease where cid = ?";
        List<TxxLease> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<TxxLease>(TxxLease.class), cid);
        return new Result(1, null, query);
    }

    @RequestMapping("getLeaseByUid")
    public List<TxxLeaseEx> getLeaseByUid(int id, int pageSize, int pageNum){
        pageNum = (pageNum - 1) * pageSize;
        return txxLeaseMapper.selectLeaseByUid(id, pageSize, pageNum);
    }

    @RequestMapping("getLeaseByUidNum")
    public int countLease(int id){
        return txxLeaseMapper.countLease(id);
    }
}
